import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  OneToMany
} from 'typeorm'

@Entity('files')
export class File {
  @PrimaryGeneratedColumn()
  id: number

  @Column()
  filename: string

  @Column()
  fileHash: string

  @Column()
  filePath: string

  @Column()
  size: number

  @Column()
  userId: number

  @Column({ nullable: true })
  mimeType: string

  @Column({ default: false })
  isPublic: boolean

  @Column({ default: 'file' })
  type: string // file、folder

  @Column({ default: null })
  parentId: number

  // 关联到父文件夹
  @ManyToOne(() => File, (file) => file.children, { nullable: true })
  @JoinColumn({ name: 'parentId' })
  parent: File

  // 子文件和子文件夹
  @OneToMany(() => File, (file) => file.parent)
  children: File[]

  @CreateDateColumn()
  createdAt: Date

  @UpdateDateColumn()
  updatedAt: Date
}
